package gwt.trading.client;

import gwt.trading.client.geometry.Padding;

/**
 * Transforms a given value to relative coordinates.
 * 
 * @author Iulian Costan (iulian.costan@gmail.com)
 * 
 */
public class Transformer {

	/* max value on x axis */
	private int xMax;
	/* size x asis in pixels */
	private int xSize;

	/* min value on y asis */
	private float yMin;
	/* max value on y asis */
	private float yMax;
	/* size of y axis in pixels */
	private int ySize;
	/* padding */
	private Padding padding;

	public Transformer(int xMax, int xSize, float yMin, float yMax, int ySize, Padding padding) {
		this.xMax = xMax;
		this.xSize = xSize;

		this.yMin = yMin;
		this.yMax = yMax;
		this.ySize = ySize;

		this.padding = padding;
	}

	/**
	 * @param xValue
	 *            the X axis value to transform
	 * @return relative position on X axis
	 */
	public int transformToXCoordinate(float xValue) {
		int realXSize = xSize - (padding.getLeft() + padding.getRight());
		int xRelative = xSize - Math.round(xValue * realXSize / xMax) - padding.getRight();
		return xRelative;
	}

	//
	// xMax ... xSize
	// xValue ... X
	//

	/**
	 * @param yValue
	 *            the Y axis value to transform
	 * @return relative value on Y axis
	 */
	public int transformToYCoordinate(float yValue) {
		return Math.round((yValue - yMin) * ySize / (yMax - yMin));
	}

	public float fromX(int xPosition) {
		return xPosition * xMax / xSize;
	}

	public float fromY(int yPosition) {
		return yPosition * yMax / ySize;
	}

	public int getMaxYPosition() {
		return ySize;
	}

	public int getMaxXPosition() {
		return xSize;
	}

}
